<script setup lang="ts">
import {markRaw, onMounted, ref} from "vue";
import type {EChartsOption, EChartsType} from "echarts";
import * as echarts from "echarts";
import {useGetBarDataApi} from "@/api";
import type {Bar} from "@/interface";


interface Emits {
  (e: 'click', name: string): void;
}
const bar = ref()
const option = ref<EChartsOption>()
const myChart = ref<EChartsType>()
const barData = ref<Bar>({
  name:[],
  value:[]
})
const emits = defineEmits<Emits>()
const init = async () => {
  const {data:res} = await useGetBarDataApi()
  barData.value = res
  myChart.value = markRaw(echarts.init(bar.value));
  draw();
}
const draw = () => {
  option.value = {
    title:{
      text:'景点热度TOP10',
      left:'center',
      textStyle:{
        color: 'rgb(238, 197, 102)'
      }
    },
    grid: {
      bottom: 120
    },
    tooltip:{},
    xAxis: {
      type: 'category',
      data: barData.value.name,
      axisLabel:{
        rotate:45,
        color: 'rgb(238, 197, 102)'
      },
      axisLine:{
        lineStyle: {
          color: 'rgb(238, 197, 102)'
        }
      }
    },
    yAxis: {
      type: 'value',
      axisLabel:{
        color: 'rgb(238, 197, 102)'
      },
      splitLine: {
        lineStyle: {
          color: 'rgb(238, 197, 102)'
        }
      }
    },
    series: [
      {
        name:'热度',
        data: barData.value.value,
        type: 'bar',
        itemStyle: {
          color: '#F9713C'
        },
      }
    ]
  }
  myChart.value.setOption(option.value, {notMerge: true});
  myChart.value.on('click', (params) => {
    // console.log('name:', params.name);
    // console.log('seriesName:', params.seriesName);
    // console.log('value:', params.value);
    // console.log('dataIndex:', params.dataIndex);
    // console.log('data:', params.data);
    emits('click', params.name)
  })
}
onMounted(() => {
  init()
})
</script>

<template>
  <div ref="bar" class="bar">
  </div>
</template>

<style scoped>
.bar{
  width: 100%;
  height: 100%;
}
</style>